package com.alibaba.sample.kantiezi.web.store.module.screen;

import com.alibaba.biz.command.Command;
import com.alibaba.biz.command.CommandSupport;
import com.alibaba.biz.command.dispatcher.CommandDispatcher;
import com.alibaba.biz.command.result.Result;

import com.alibaba.sample.kantiezi.dal.dataobject.Category;
import com.alibaba.sample.kantiezi.dal.dataobject.Product;

import com.alibaba.service.template.TemplateContext;

import com.alibaba.turbine.module.screen.TemplateScreen;
import com.alibaba.turbine.service.rundata.RunData;

import com.alibaba.webx.WebxException;

import java.util.List;

public abstract class ItemList extends TemplateScreen {
    protected abstract CommandDispatcher getCommandDispatcher();

    protected void execute(RunData rundata, TemplateContext context)
            throws WebxException {
        Command command = new CommandSupport("itemList");

        command.getParameters().put("productId", rundata.getParameters().getString("productId"));

        Result result = getCommandDispatcher().execute(command);

        if (result.isSuccess()) {
            Category category = (Category) result.getModels().get("category");
            Product  product  = (Product) result.getModels().get("product");
            List     items    = (List) result.getDefaultModel();

            context.put("category", category);
            context.put("product", product);
            context.put("items", items);
        }
    }
}
